package dom4j_xml;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Iterator;

public class Dom4jParse {
    private static   Document document;
    public static void save() throws DocumentException {
        SAXReader saxReader=new SAXReader();
        document=saxReader.read("E:\\project-java65-parents\\.idea\\socre.xml");

    }
    public static void save1() throws IOException {
        OutputFormat format=OutputFormat.createCompactFormat();
        XMLWriter writer=new XMLWriter(new OutputStreamWriter(new FileOutputStream("E:\\project-java65-parents\\.idea\\socre.xml") ),format);
        writer.write(document);
        writer.close();
    }
    public static  void showInfo() throws DocumentException {
       save();
        Element root=document.getRootElement();
        Iterator iterator=root.elementIterator();
        while (iterator.hasNext()){
            Element stu=(Element)  iterator.next();
            String id=stu.attributeValue("id");
            System.out.println("id"+id);
            Iterator iterator1=stu.elementIterator();
            while (iterator1.hasNext()){
               Element info=(Element)iterator1.next();
                String infoText=info.getText();
                System.out.println(info.getName()+":"+infoText);
            }
        }

    }

    public static void delete() throws DocumentException, IOException {
        save();
        Element root=document.getRootElement();
        Iterator iterator=root.elementIterator();
        while (iterator.hasNext()) {
            Element stu = (Element) iterator.next();
            String id = stu.attributeValue("id");
            if (id.equalsIgnoreCase("1")){
                stu.getParent().remove(stu);
            }
        }

        save1();
    }
    public static void change() throws DocumentException, IOException {
        save();
        Element root=document.getRootElement();
        Iterator iterator=root.elementIterator();
        while (iterator.hasNext()) {
            Element stu = (Element) iterator.next();
            String id = stu.attributeValue("id");
            if (id.equalsIgnoreCase("2")){
                Iterator iterator1=stu.elementIterator();
                while (iterator1.hasNext()){
                    Element info=(Element)iterator1.next();
                    String name=info.getName();
                    if (name.equalsIgnoreCase("score")){
                        info.setText("60");
                    }
                }
            }
        }

        save1();
    }

    public static void add() throws DocumentException, IOException {
        save();
        Element root=document.getRootElement();
        Element stu=root.addElement("student");
        stu.addAttribute("id","5");
        Element name=stu.addElement("name");
        name.setText("杨哥");
        Element course=stu.addElement("course");
        course.setText("吃好吃的");
        Element score=stu.addElement("score");
        score.setText("100");
        save1();


    }



    public static void main(String[] args) throws DocumentException, IOException {
        showInfo();
        delete();
        change();
        add();
        showInfo();
    }
}
